package zhaoq_hero.superapp.providers;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.net.Uri;

/**
 * 浏览器相关的内容提供者
 */
public class BrowserProvider extends ContentProvider {

    private DBHelper dbHelper;

    private static final UriMatcher match;
    public static final int CODE_HISTORY = 1;
    public static final int CODE_BOOK = 2;


    static{
        match = new UriMatcher(UriMatcher.NO_MATCH);

        //添加匹配规则：
        /**
         * 参数一：需要匹配的authority,指定的authority，如果有多个authority支持，那么也只能够匹配指定的那一个
         */
        match.addURI("*","history/", CODE_HISTORY);//content：//authority/history
        match.addURI("*", "book/", CODE_BOOK);
    }

    @Override
    public boolean onCreate() {
        dbHelper = new DBHelper(getContext());
        return true;//代表加载成功  false 创建失败  不允许使用
    }

    public BrowserProvider() {
    }


    /**
     * 查询操作
     * @param uri
     * @param projection
     * @param selection
     * @param selectionArgs
     * @param sortOrder
     * @return
     */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Cursor ret = null;
        if (uri != null) {
            int code = BrowserProvider.match.match(uri);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            switch (code) {
                case CODE_HISTORY:
                    //db.rawQuery("select ",null);
                    ret = db.query(DataContract.TABLE_HISTORY,
                            projection,
                            selection,
                            selectionArgs,
                            null,
                            null,
                            sortOrder);
                    break;
                case CODE_BOOK:
                    ret = db.query(DataContract.TABLE_BOOK,
                            projection,
                            selection,
                            selectionArgs,
                            null,
                            null,
                            sortOrder);
                    break;
            }
        }
        return ret;
    }



    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int ret = -1;

        if (uri != null) {
            int code = match.match(uri);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            switch (code) {
                case CODE_HISTORY:
                    ret = db.delete(DataContract.TABLE_HISTORY,
                            selection,
                            selectionArgs
                    );

                    break;
                case CODE_BOOK:
                    ret = db.delete(DataContract.TABLE_BOOK,
                            selection,
                            selectionArgs);
                    break;

            }
        }
        return ret;
    }


    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * 添加数据   根据uri来添加不同的数据  数据内容放在ContentValues  包含数据内容
     * @param uri
     * @param values
     * @return uri 代表新添加的那一条记录的访问uri  该uri可用于删除
     */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri ret = null;
        if (uri != null) {
            if (values == null) {
                values = new ContentValues();
            }
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            // TODO 匹配URI 到底处理哪一张表格
            long rid = -1;
            switch (match.match(uri)) {
                case CODE_HISTORY://添加历史记录
                    rid = db.insert(DataContract.TABLE_HISTORY, null, values);
                    break;
                case CODE_BOOK://添加书签
                    rid = db.insert(DataContract.TABLE_BOOK, null, values);
                    break;
            }
            if (rid > -1) {
                // TODO 代表记录添加成功
                // 定义content://xxxx/history/id代表新添加的记录
                ret = ContentUris.withAppendedId(uri, rid);

            }
            db.close();
        }


        return ret;
    }



    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int ret = -1;
        if (uri != null) {
            SQLiteDatabase database = dbHelper.getWritableDatabase();
            int code = BrowserProvider.match.match(uri);
            switch (code) {
                case CODE_HISTORY:
                    ret = database.update(
                            DataContract.TABLE_HISTORY,
                            values,
                            selection,
                            selectionArgs
                    );
                    break;
                case CODE_BOOK:
                    ret = database.update(
                            DataContract.TABLE_BOOK,
                            values,
                            selection,
                            selectionArgs
                    );
                    break;
            }

        }
        return ret;
    }
}
